<?php

namespace App\Kuafu\Shop\Service;

use App\Kuafu\Audit\Enum\AuditEventCodeEnum;
use App\Kuafu\Base\Service\BaseService;
use App\Kuafu\Common\Service\CommonService;
use App\Kuafu\Pay\Model\PayStreamModel;
use App\Kuafu\Shop\Enum\ShopCacheEnum;
use App\Kuafu\Shop\Enum\ShopCodeEnum;
use App\Kuafu\Shop\Exceptions\ShopException;
use App\Kuafu\Shop\Model\Shop;
use App\Kuafu\Shop\Model\ShopPaymentsModel;
use Illuminate\Support\Facades\Cache;
use function Symfony\Component\String\s;

/**
 * 店铺 开票信息
 */
class ShopInvoiceService extends BaseService
{
    /**
     * 获取开票信息的数据模板
     *
     * @param string $type
     * @return array|string[]
     */
    public static function getInvoiceTemplate($type = 'moredo')
    {
        $invoiceTemplate = [
            'moredo' => [
                'invoice'      => '纳税人识别号',
                'kpjh'         => '开票机号',
                'fee'          => '适用税率',
                'isDiscount'   => '是否优惠',
                'discountName' => '优惠类型',
                'name'         => '对接商名称',
                'address'      => '联系地址',
                'tel'          => '联系电话',
                'contact'      => '联系人',
            ]
        ];
        return $invoiceTemplate[$type] ?? [];
    }


    /**
     * @param $params
     * @param $uid
     * @param $shopId
     * @param int $payChannel
     * @throws ShopException
     */
    public static function shopInvoiceUpdate($params, $shopId)
    {
        try {
            $shopInvoiceJson = json_encode(
                $params, 256
            );

            ShopService::updateShopInfo(['id' => $shopId], ['invoice' => $shopInvoiceJson]);
        } catch (\Exception $e) {
            throw new ShopException(ShopCodeEnum::ERR_SHOP_DB, $e->getMessage());
        }

    }

    /**
     * 获取开票信息
     *
     * @param $shopId
     * @return array|mixed
     */
    public static function getShopInvoiceInfo($shopId)
    {
        $shopInfo = ShopService::getShopInfo($shopId);

        $invoiceInfo = json_decode($shopInfo->invoice, 1);

        if (empty($invoiceInfo)) {
            return [];
        } else {
            return $invoiceInfo[$invoiceInfo['selected']];
        }
    }

}
